//
//  LPShowMenuView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPShowMenuView.h"

@interface LPShowMenuView ()
/**  下划线  */
@property (strong, nonatomic) UIView  *underline;
/**  button宽度  */
@property (assign, nonatomic) CGFloat  buttonWITH;
/**  上一个button  */
@property (strong, nonatomic) UIButton  *lastButton;
/**  上一个选中的button的TAG  */
@property (assign, nonatomic) NSInteger  lastButtonTag;
/**  buttong数组  */
@property (strong, nonatomic) NSMutableArray  *buttonArray;


@end
@implementation LPShowMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        TenGrayShadowView *three = [TenGrayShadowView new];
        [self addSubview:three];
        three.sd_layout.bottomEqualToView(self)
        .leftEqualToView(self)
        .rightEqualToView(self)
        .heightIs(3 * [UIScreen zj_screenHScale]);
        
        [self layoutUI];
        [self addSubview:self.underline];
    }
    return self;
}




#pragma mark - 界面
- (void)layoutUI{
    NSArray *titles = @[@"详情",@"动态",@"报道"];
    NSArray *images = @[@"wxy_xiangqing_d",@"wxy_dongtai_d",@"wxy_baodao_d"];
     NSArray *selectedImages = @[@"wxy_xiangqing_h",@"wxy_dongtai_h",@"wxy_baodao_h"];

    CGFloat buttonWith = [UIScreen zj_screenWith] / titles.count;
    self.buttonWITH = buttonWith;
    for (int i = 0; i < titles.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * buttonWith, 7 * [UIScreen zj_screenHScale], buttonWith, 40 * [UIScreen zj_screenHScale])];
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
        button.imageView.contentMode = UIViewContentModeCenter;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor themeColor] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:selectedImages[i]] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        if (i == 0) {
            button.selected = YES;
            self.lastButton = button;
            self.lastButtonTag = i;
        }
        [self.buttonArray addObject:button];
    }
    
}


#pragma mark - 私有方法

- (void)laoutUnderLineIndex:(NSInteger)index{
    CGFloat width = self.buttonWITH * 0.25 + self.buttonWITH * index;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = self.underline.frame;
        frame.origin.x = width;
        self.underline.frame = frame;
        
        
    } completion:^(BOOL finished) {
        if (self.lastButtonTag != index) {
            self.lastButton.selected = NO;
            UIButton *button = self.buttonArray[index];
            button.selected = YES;
            self.lastButton = button;
            self.lastButtonTag = index;
        }
    }];
}

#pragma mark - 监听事件
- (void)buttonClick:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate LPShowMenuWithView:self buttonIndex:sender.tag];
    }
}

#pragma mark - 协议代理


#pragma mark - getter
- (UIView *)underline{
    if (_underline == nil) {
        _underline = [[UIView alloc] initWithFrame:CGRectMake(self.buttonWITH * 0.25, 45 * [UIScreen zj_screenHScale], self.buttonWITH * 0.5, 2 * [UIScreen zj_screenHScale])];
        _underline.backgroundColor = [UIColor themeColor];
    }
    return _underline;
}

- (NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}
@end
